package balmysundaycandy.more.low.level.memcached;

import com.google.appengine.api.memcache.MemcacheServicePb.MemcacheDeleteRequest;
import com.google.appengine.api.memcache.MemcacheServicePb.MemcacheDeleteResponse;
import com.google.appengine.api.memcache.MemcacheServicePb.MemcacheFlushRequest;
import com.google.appengine.api.memcache.MemcacheServicePb.MemcacheFlushResponse;
import com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest;
import com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetResponse;
import com.google.appengine.api.memcache.MemcacheServicePb.MemcacheIncrementRequest;
import com.google.appengine.api.memcache.MemcacheServicePb.MemcacheIncrementResponse;
import com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest;
import com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse;
import com.google.appengine.api.memcache.MemcacheServicePb.MemcacheStatsRequest;
import com.google.appengine.api.memcache.MemcacheServicePb.MemcacheStatsResponse;
import com.google.appengine.repackaged.com.google.protobuf.AbstractMessage;
import com.google.appengine.repackaged.com.google.protobuf.InvalidProtocolBufferException;
import com.google.apphosting.api.ApiProxy;

/**
 * @author marblejenka
 * 
 * @param <Request>
 * @param <Response>
 */
abstract class MemcacheOperation<Request extends AbstractMessage, Response extends AbstractMessage> {
	private static final String packageName = "memcache";

	public abstract Response call(Request request);

	public static final class GetOperation extends MemcacheOperation<MemcacheGetRequest, MemcacheGetResponse> {

		@Override
		public MemcacheGetResponse call(MemcacheGetRequest request) {
			MemcacheGetResponse.Builder response = MemcacheGetResponse.newBuilder();
			try {
				response.mergeFrom(ApiProxy.makeSyncCall(packageName, "Add", request.toByteArray()));
			} catch (InvalidProtocolBufferException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response.build();
		}

	}

	public static final class SetOperation extends MemcacheOperation<MemcacheSetRequest, MemcacheSetResponse> {

		@Override
		public MemcacheSetResponse call(MemcacheSetRequest request) {
			MemcacheSetResponse.Builder response = MemcacheSetResponse.newBuilder();
			try {
				response.mergeFrom(ApiProxy.makeSyncCall(packageName, "Set", request.toByteArray()));
			} catch (InvalidProtocolBufferException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response.build();
		}

	}

	public static final class DeleteOperation extends MemcacheOperation<MemcacheDeleteRequest, MemcacheDeleteResponse> {

		@Override
		public MemcacheDeleteResponse call(MemcacheDeleteRequest request) {
			MemcacheDeleteResponse.Builder response = MemcacheDeleteResponse.newBuilder();
			try {
				response.mergeFrom(ApiProxy.makeSyncCall(packageName, "Delete", request.toByteArray()));
			} catch (InvalidProtocolBufferException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response.build();
		}

	}

	public static final class IncrementOperation extends MemcacheOperation<MemcacheIncrementRequest, MemcacheIncrementResponse> {

		@Override
		public MemcacheIncrementResponse call(MemcacheIncrementRequest request) {
			MemcacheIncrementResponse.Builder response = MemcacheIncrementResponse.newBuilder();
			try {
				response.mergeFrom(ApiProxy.makeSyncCall(packageName, "Increment", request.toByteArray()));
			} catch (InvalidProtocolBufferException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response.build();
		}

	}

	public static final class FlushAllOperation extends MemcacheOperation<MemcacheFlushRequest, MemcacheFlushResponse> {

		@Override
		public MemcacheFlushResponse call(MemcacheFlushRequest request) {
			MemcacheFlushResponse.Builder response = MemcacheFlushResponse.newBuilder();
			try {
				response.mergeFrom(ApiProxy.makeSyncCall(packageName, "FlashAll", request.toByteArray()));
			} catch (InvalidProtocolBufferException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response.build();
		}
	}

	public static final class StatsOperation extends MemcacheOperation<MemcacheStatsRequest, MemcacheStatsResponse> {

		@Override
		public MemcacheStatsResponse call(MemcacheStatsRequest request) {
			MemcacheStatsResponse.Builder response = MemcacheStatsResponse.newBuilder();
			try {
				response.mergeFrom(ApiProxy.makeSyncCall(packageName, "Stats", request.toByteArray()));
			} catch (InvalidProtocolBufferException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response.build();
		}
	}
}
